//
//  FlutterNativePlugin.m
//  Runner
//
//  Created by junyi tan  on 2020/3/12.
//  Copyright © 2020 The Chromium Authors. All rights reserved.
//

#import "FlutterNativePlugin.h"

@implementation FlutterNativePlugin
//----------------  FlutterMethodChannel  --------------------------
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"message_channel_test" binaryMessenger:[registrar messenger]];
    FlutterNativePlugin *instance = [[FlutterNativePlugin alloc]init];
    [registrar addMethodCallDelegate:instance channel:channel];
    [self registerPostTimerWithRegistrar:registrar];
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    if ([call.method isEqualToString:@"commeon"]) {
        NSLog(@"收到了commeon方法调用请求");
        result(@"么么哒");
    }else {
      NSLog(@"收到了其他方法调用请求");
        result(FlutterMethodNotImplemented);
    }
}

//----------------------   FlutterBasicMessageChannel  -------------------------------
+(void) registerPostTimerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar{
    FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel messageChannelWithName:@"run_time" binaryMessenger:[registrar messenger]];

    long start = [self getNow];
    NSLog(@"开始计时");
    [NSTimer scheduledTimerWithTimeInterval:5 repeats:true block:^(NSTimer * _Nonnull timer) {
        long run = [self getNow] - start;
        NSLog(@"发送消息");
        [channel sendMessage:[NSNumber numberWithLong:run]];
        
        if (run > 100){
            [timer invalidate];
        }
    }];
}

+(long)getNow{
    NSDate *date = [NSDate date];
    long timeStamp = [date timeIntervalSince1970];
    return timeStamp;
}


@end
